package cn.hl.hm.service.impl;



import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.hl.hm.dao.TFeedbackEntityMapper;
import cn.hl.hm.dao.TReviewsEntityMapper;
import cn.hl.hm.entity.Driver;
import cn.hl.hm.entity.TFeedbackEntity;
import cn.hl.hm.entity.TReviewsEntity;
import cn.hl.hm.service.DriverService;
import cn.hl.hm.service.TFeedbackService;
import cn.hl.hm.util.JacksonJsonUtil;



@Component
public class TFeedbackServiceImpl implements TFeedbackService {
    private static final Logger logger = LoggerFactory
            .getLogger(TFeedbackServiceImpl.class);


    @Autowired
    TFeedbackEntityMapper tFeedbackEntityMapper;
//    @Autowired
//    TUserEntityMapper tUserEntityMapper;
    @Autowired
    TReviewsEntityMapper tReviewsEntityMapper;
    @Autowired
    DriverService driverService;


	@Override
	public TFeedbackEntity insert(Map<String, String> map) {
		TFeedbackEntity i=null;
		if(null ==map || "null".equals(map) || !(map.size() > 0)){
			return i;
		}
		String userId=map.get("userId");
		String appId=map.get("appId");
		String apiVersion=map.get("apiVersion");
		String feedbackContent=map.get("feedbackContent");
		String feedbackType=map.get("feedbackType");
		String source=map.get("source");
		 if(null == source || "null".equals(source)||null == feedbackType || "null".equals(feedbackType)|| null == userId || "null".equals(userId) || null == appId || "null".equals(appId) || null == apiVersion || "null".equals(apiVersion)|| null == feedbackContent || "null".equals(feedbackContent)){
		 return i;
		 }
		 Driver d = new Driver();
		 d.setId(Integer.parseInt(userId));
		 List <Driver>   driverL=driverService.selectDriver(d) ;
		 
//		 TUserEntity tue=null;
		TFeedbackEntity tfe=new TFeedbackEntity();
		
		   tfe.setAppId(appId);
		   tfe.setVersion(apiVersion);
		   tfe.setUserId(Integer.parseInt(userId));
		   tfe.setContent(feedbackContent);
		   if(feedbackType !=null && !("null".equals(feedbackType))){
		   tfe.setFeedbackType(Integer.parseInt(feedbackType));
		   }
		   tfe.setCreateTime(new Date());
		   tfe.setWwidId(source);//2司机 1普通用户代替
		   
		   if(driverL!=null && driverL.size()>0){
			   Driver dd =  driverL.get(0);
			   if(dd.getDriverPhone()  !=null && !("null".equals(dd.getDriverPhone()  ))){
				   tfe.setPhone(dd.getDriverPhone());
			   }
			   if(dd.getName() !=null && !("null".equals(dd.getName()))){
				   tfe.setName(dd.getName() );
		   }
		   }
		  tFeedbackEntityMapper.insertSelective(tfe);
		  i=new TFeedbackEntity();
		  i.setCreateTime(new Date());
		  i.setId(tfe.getId());
		
		return i;
	}


	@Override
	public List<TFeedbackEntity> getTFeedbackList(Map<String, String> map) {
		logger.info(JacksonJsonUtil.beanToJson(map));
		List<TFeedbackEntity>  tceList=null;
		  if(null ==map || !(map.size()>0) ||"null".equals(map)){
			  return tceList;
		  }     
		  String userId=map.get("userId");
		  String pageId=map.get("pageId");
		  String pageCount=map.get("pageCount"); 
		  String source=map.get("source");
		  if(null == userId || "null".equals(userId) || null == source || "null".equals(source)){
			  return tceList;
		  }
		  Map<String,Object>resultMap=new HashMap<>();
		  resultMap.put("userId", userId);
		  resultMap.put("wwidId", source);
		  if(pageId != null && !("null".equals(pageId))){
			  resultMap.put("limit", Integer.parseInt(pageId)*Integer.parseInt(pageCount));
		  }
		  if(pageCount != null && !("null".equals(pageCount))){
			  resultMap.put("pageSize", Integer.parseInt(pageCount));
		  }
		  tceList=tFeedbackEntityMapper.selectTTFeedbackList(resultMap);
		return tceList;
	}


	@Override
	public List<TReviewsEntity> selectDriverReviewList(Map<String, String> map) {
		logger.info(JacksonJsonUtil.beanToJson(map));
		List<TReviewsEntity>  tceList=null;
		  if(null ==map || !(map.size()>0) ||"null".equals(map)){
			  return tceList;
		  }     
		  String userId=map.get("userId");
		  String pageId=map.get("pageId");
		  String pageCount=map.get("pageCount"); 
		  String source=map.get("source");
		  if(null == userId || "null".equals(userId) || null == source || "null".equals(source)){
			  return tceList;
		  }
		  Map<String,Object>resultMap=new HashMap<>();
		  if("1".equals(source)){//普通用户
			  resultMap.put("senderId", userId);
		  }else if("2".equals(source)){//司机
			  resultMap.put("driverId", userId);
		  }
		  if(pageId != null && !("null".equals(pageId))){
			  resultMap.put("limit", Integer.parseInt(pageId)*Integer.parseInt(pageCount));
		  }
		  if(pageCount != null && !("null".equals(pageCount))){
			  resultMap.put("pageSize", Integer.parseInt(pageCount));
		  }
		 tceList=tReviewsEntityMapper.selectByReviewsList(resultMap);
		return tceList;
	}


	@Override
	public List<TFeedbackEntity> backstageFeedbackList(String phone,String createTime,
			Integer limit, Integer pageSize) {
		// TODO Auto-generated method stub
		return tFeedbackEntityMapper.selectbackstageFeedbackList(phone, createTime, limit, pageSize);
	}


	@Override
	public TFeedbackEntity selectfeedbackOne(Integer id) {
		// TODO Auto-generated method stub
		return tFeedbackEntityMapper.selectfeedbackOne(id);
	}
   


}
